package com.liang.dao;

import com.liang.pojo.Department;
import com.liang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//员工dao
@Repository
public class EmployeeDao {
    @Autowired
    DepartmentDao departmentDao;//注意！员工有所属的部门，需要注入department！
    //模拟数据库中的数据
    private static Map<Integer,Employee> employees= null;
    static {
        employees = new HashMap<>();//创建一个员工表
        employees.put(1001,new Employee(1001,"AA",new Department(101,"教学部"),0,"A877815207@qq.com"));
        employees.put(1002,new Employee(1002,"BB",new Department(102,"市场部"),1,"B877815207@qq.com"));
        employees.put(1003,new Employee(1003,"CC",new Department(103,"教研部"),0,"C877815207@qq.com"));
        employees.put(1004,new Employee(1004,"DD",new Department(104,"运营部"),1,"D877815207@qq.com"));
        employees.put(1005,new Employee(1005,"EE",new Department(105,"后勤部"),0,"E877815207@qq.com"));
    }

    //主键自增！
    private static Integer initId = 1006;
    //增加一个员工
    public void add(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    //查询全部员工信息
    public Collection<Employee> getEmployees(){
        return employees.values();
    }

    //根据id查询员工信息
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    //删除员工信息
    public void delete(Integer id){
        employees.remove(id);
    }
}
